package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.BdRepayDetailDao;
import com.fdb.efp.nls.service.domain.BdRepayDetail;
import com.fdb.efp.nls.service.facade.BdRepayDetailService;
import com.fdb.efp.nls.service.facade.BdRepayDetailTempService;
import com.fdb.efp.nls.service.vo.BdRepayDetailTempVO;
import com.fdb.efp.nls.service.vo.BdRepayDetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 还款明细分期表
 * <p>
 * 
 * @author ligm
 * @date 2019-03-07
 */
@Service("bdRepayDetailService")
public class BdRepayDetailServiceImpl implements BdRepayDetailService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BdRepayDetailServiceImpl.class);

	@Autowired
	private BdRepayDetailDao bdRepayDetailDao;
	@Autowired
	@Qualifier("bdRepayDetailTempService")
	private BdRepayDetailTempService bdRepayDetailTempService;

	/**
	 * 新增操作
	 */
	public int insertBdRepayDetail(BdRepayDetailVO inBdRepayDetailVo) {
		logger.debug("当前新增数据为:" + inBdRepayDetailVo.toString());
		int num = 0;
		try {
			BdRepayDetail bdRepayDetail = new BdRepayDetail();
			beanCopy(inBdRepayDetailVo, bdRepayDetail);
			num = bdRepayDetailDao.insertBdRepayDetail(bdRepayDetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BdRepayDetailVO inBdRepayDetailVo) {
		logger.debug("当前删除数据条件为:" + inBdRepayDetailVo);
		int num = 0;
		try {
			BdRepayDetail bdRepayDetail = new BdRepayDetail();
			beanCopy(inBdRepayDetailVo, bdRepayDetail);
			num = bdRepayDetailDao.deleteByPk(bdRepayDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdRepayDetailVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(BdRepayDetailVO inBdRepayDetailVo) {
		logger.debug("当前修改数据为:" + inBdRepayDetailVo.toString());
		int num = 0;
		try {
			BdRepayDetail bdRepayDetail = new BdRepayDetail();
			beanCopy(inBdRepayDetailVo, bdRepayDetail);
			num = bdRepayDetailDao.updateByPk(bdRepayDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdRepayDetailVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public BdRepayDetailVO queryByPk(BdRepayDetailVO inBdRepayDetailVo) {

		logger.debug("当前查询参数信息为:" + inBdRepayDetailVo);
		try {
			BdRepayDetail queryBdRepayDetail = new BdRepayDetail();
			beanCopy(inBdRepayDetailVo, queryBdRepayDetail);
			BdRepayDetail queryRslBdRepayDetail = bdRepayDetailDao.queryByPk(queryBdRepayDetail);
			if (Objects.nonNull(queryRslBdRepayDetail)) {
				BdRepayDetailVO outBdRepayDetailVo = beanCopy(queryRslBdRepayDetail, new BdRepayDetailVO());
				logger.debug("当前查询结果为:" + outBdRepayDetailVo.toString());
				return outBdRepayDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdRepayDetailVO> queryAllOwner(BdRepayDetailVO bdRepayDetailVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BdRepayDetailVO> list = null;
		try {
			List<BdRepayDetail> bdRepayDetails = bdRepayDetailDao.queryAllOwnerByPage(bdRepayDetailVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + bdRepayDetails.size());
			pageSet(bdRepayDetails, bdRepayDetailVo);
			list = (List<BdRepayDetailVO>) beansCopy(bdRepayDetails, BdRepayDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdRepayDetailVO> queryAllCurrOrg(BdRepayDetailVO bdRepayDetailVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BdRepayDetail> bdRepayDetails = bdRepayDetailDao.queryAllCurrOrgByPage(bdRepayDetailVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + bdRepayDetails.size());
		List<BdRepayDetailVO> list = null;
		try {
			pageSet(bdRepayDetails, bdRepayDetailVo);
			list = (List<BdRepayDetailVO>) beansCopy(bdRepayDetails, BdRepayDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdRepayDetailVO> queryAllCurrDownOrg(BdRepayDetailVO bdRepayDetailVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BdRepayDetail> bdRepayDetails = bdRepayDetailDao.queryAllCurrDownOrgByPage(bdRepayDetailVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bdRepayDetails.size());
		List<BdRepayDetailVO> list = null;
		try {
			pageSet(bdRepayDetails, bdRepayDetailVo);
			list = (List<BdRepayDetailVO>) beansCopy(bdRepayDetails, BdRepayDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 
	 * 批量新增还款明细分期信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchInsertBdRepayDetail(List<BdRepayDetailTempVO> addList) {
		logger.debug("当前新增数据条数为:" + addList.size());
		List<BdRepayDetail> BdRepayDetailList = new ArrayList<BdRepayDetail>();
		int num = 0;
		try {
			num = bdRepayDetailDao.batchInsertBdRepayDetail(BdRepayDetailList);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 
	 * 批量更新还款明细分期信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchUpdateBdRepayDetail(List<BdRepayDetailTempVO> updateList) {
		logger.debug("当前修改数据为:" + updateList.size() + "条");
		List<BdRepayDetail> BdRepayDetailList = new ArrayList<BdRepayDetail>();
		int num = 0;
		try {
			num = bdRepayDetailDao.batchUpdateBdRepayDetail(BdRepayDetailList);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("修改的数据条数为" + num);
		return num;
	}

	/**
	 * 
	 * 同步还款明细临时表数据到还款明细正式表有数据则更新，无则新增 <br>
	 * 0.1:ludayong:2019年3月23日 下午12:09:28:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月23日 下午12:09:28
	 * @date 2019年3月23日 下午12:09:28
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateBdRepayDetail(List<BdRepayDetailVO> list) {
		LocalDate today = LocalDate.now();
		String msg1 = "【还款明细明细表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<BdRepayDetail> bdRepayDetaillList = new ArrayList<BdRepayDetail>();
		int num = 0;
		try {
			bdRepayDetaillList = (List<BdRepayDetail>) beansCopy(list, BdRepayDetail.class);
			num = bdRepayDetailDao.insertOrUpdateBdRepayDetail(bdRepayDetaillList);
			String num_msg = "【还款明细明细表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【还款明细明细表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
}
